package com.epichust.bootumpp.controller;

import com.epichust.bootumpp.model.bo.ReturnBO;
import com.epichust.bootumpp.model.bo.Umpp5PlanOrderBO;
import com.epichust.bootumpp.model.bo.Umpp5PlanOrderPublishBO;
import com.epichust.bootumpp.model.entity.Umpp5PlanOrder;
import com.epichust.bootumpp.service.IUmpp5PlanOrderService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2021/1/12 11:49
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("api/planOrder")
public class Umpp5PlanOrderController  {

    @Autowired
    private IUmpp5PlanOrderService iUmpp5PlanOrderService;

    @RequestMapping(value = "/getAll", method = {RequestMethod.POST})
    public String getAllUmpp5PlanOrder(@RequestBody Map<String,Object> map){
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            ReturnBO bo = new ReturnBO();
            List<Umpp5PlanOrderBO> list = iUmpp5PlanOrderService.getAllUmpp5PlanOrder(map);
            bo.setState(0);
            bo.setObject(list);
            return gson.toJson(bo);
        }catch (Exception e){
            e.printStackTrace();
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("查询失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }

    /**
     * 订单发布
     * @param list
     * @return
     */
    @RequestMapping(value="/publishOrder",method = {RequestMethod.POST})
    public String publishOrder(@RequestBody List<Umpp5PlanOrderPublishBO> list){
        try{
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            ReturnBO bo = new ReturnBO();
            iUmpp5PlanOrderService.publishOrder(list);
            bo.setState(0);
            bo.setObject("发布成功");
            return gson.toJson(bo);
        }catch (Exception e){
            e.printStackTrace();
            // 新建返回对象
            Gson gson = new GsonBuilder().serializeNulls().create();
            ReturnBO bo = new ReturnBO();
            bo.setState(1);
            bo.setObject("发布失败："+e.getMessage());
            return gson.toJson(bo);
        }
    }
}
